package com.example.impl;

import com.example.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * @Description
 * @Date 2021/7/11
 * @Created by Jonathan
 */
@Slf4j
public class Test {
    public static void main(String[] args) {
        String content = "${Signatory}总部位于中国${Signatory}" +
                "自由贸易${Signatory}${Signatory}验区，依法从事商${SystemName}${SystemName}期货经纪、金融期货经纪" +
                "${SystemName}${SystemName}期货交易${SystemName}";
        /*String[] split = content.split("\\$\\{Signatory}|\\$\\{SystemName}");
        System.out.println(content);
        //System.out.println(Arrays.toString(split));
        List<String> result = new ArrayList<>();
        for (String str : split) {
            //System.out.println(str);
            if (str.length() >= 1)
                result.add(str);
            Map<String, Object> map = findFirstVariableAndIndex(content);
            String variable = (String) map.get("variable");
            //System.out.println(variable);
            result.add(variable);
            content = content.substring(str.length() + variable.length());
        }*/
        System.out.println(split(content));
        ConcurrentSkipListMap<String, String> map = new ConcurrentSkipListMap<>();
        map.put("hello", "world");
        log.info("map:{}", JsonUtil.pojoToJson(map));
        //test分支第一次提交
        //test分支第二次提交
    }

    private static List<String> split(String content) {
        String[] split = content.split("\\$\\{Signatory}|\\$\\{SystemName}");
        System.out.println(content);
        List<String> result = new ArrayList<>();
        for (String str : split) {
            if (str.length() >= 1)
                result.add(str);
            Map<String, Object> map = findFirstVariableAndIndex(content);
            String variable = (String) map.get("variable");
            result.add(variable);
            content = content.substring(str.length() + variable.length());
        }
        return result;
    }

    private static Map<String, Object> findFirstVariableAndIndex(String content) {
        Map<String, Object> map = new HashMap<>();
        Set<String> set = new HashSet<>();
        set.add("${Signatory}");
        set.add("${SystemName}");
        set.add("${Subject}");
        set.add("${Reason}");
        int minVariableIndex = Integer.MAX_VALUE;
        String variable = "";
        for (String key : set) {
            int index = content.indexOf(key);
            if (index >= 0 && index < minVariableIndex) {
                variable = key;
                minVariableIndex = index;
            }
        }
        map.put("variable", variable);
        map.put("minVariableIndex", minVariableIndex);
        return map;
    }
}
